package cn.cjlmonster.demo02.decoration;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import cn.cjlmonster.demo02.utils.DpUtils;

/**
 * This project is Demo02
 * Created by cjl on 2021/11/7 6:06 下午.
 */

public class BottomLineDecoration extends RecyclerView.ItemDecoration {

    private Context context;
    private int mBottomHeight;
    private Paint mPaint;

    public BottomLineDecoration(Context context) {
        this.context = context;
        this.mBottomHeight = DpUtils.dpToPx(context, 1);
        mPaint = new Paint();
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(mBottomHeight);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setColor(0xffd0d0d0);
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        outRect.set(0, 0, 0, mBottomHeight);
    }

    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDraw(c, parent, state);
        int itemCount = parent.getChildCount();
        for (int i = 0; i < itemCount; i++) {
            View child = parent.getChildAt(i);
            float startX = child.getLeft();
            float startY = child.getBottom() + (mBottomHeight / 2f);
            float stopX = child.getRight();
            float stopY = child.getBottom() + (mBottomHeight / 2f);
            c.drawLine(startX, startY, stopX, stopY, mPaint);
        }
    }
}
